package com.ruoyi.seedlingSys.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.seedlingSys.domain.Qr;
import com.ruoyi.seedlingSys.service.IQrService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.annotation.Anonymous;

/**
 * 二维码管理Controller
 * 
 * @author ruoyi
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/seedlingSys/qr")
public class QrController extends BaseController {
    @Autowired
    private IQrService qrService;

    /**
     * 查询二维码管理列表
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qr qr) {
        startPage();
        List<Qr> list = qrService.selectQrList(qr);
        return getDataTable(list);
    }

    /**
     * 导出二维码管理列表
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:export')")
    @Log(title = "二维码管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qr qr) {
        List<Qr> list = qrService.selectQrList(qr);
        ExcelUtil<Qr> util = new ExcelUtil<Qr>(Qr.class);
        util.exportExcel(response, list, "二维码管理数据");
    }

    /**
     * 获取二维码管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:query')")
    @GetMapping(value = "/{qrID}")
    public AjaxResult getInfo(@PathVariable("qrID") Long qrID) {
        return success(qrService.selectQrByQrID(qrID));
    }

    /**
     * 新增二维码管理
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:add')")
    @Log(title = "二维码管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qr qr) {
        return toAjax(qrService.insertQr(qr));
    }

    /**
     * 修改二维码管理
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:edit')")
    @Log(title = "二维码管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qr qr) {
        return toAjax(qrService.updateQr(qr));
    }

    /**
     * 删除二维码管理
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:remove')")
    @Log(title = "二维码管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qrIDs}")
    public AjaxResult remove(@PathVariable Long[] qrIDs) {
        return toAjax(qrService.deleteQrByQrIDs(qrIDs));
    }

    /**
     * 批量新增二维码
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:qr:add')")
    @Log(title = "二维码管理", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<Qr> qrList) {
        return toAjax(qrService.batchInsertQr(qrList));
    }

    /**
     * 获取追溯信息
     */
    @GetMapping("/trace/{code}")
    @Anonymous
    public AjaxResult getTraceInfo(@PathVariable("code") String code) {
        Qr qr = new Qr();
        qr.setQrUniqueCode(code);
        List<Qr> list = qrService.selectQrList(qr);
        return success(list.isEmpty() ? null : list.get(0));
    }
}
